//
//  SuyouSearchTableViewCell.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/2.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "SuyouSearchTableViewCell.h"
#import "Common.h"

@interface SuyouSearchTableViewCell ()

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UIView *txtView;
@property (nonatomic, strong)UIView *bgView;

@end

@implementation SuyouSearchTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        
        self.backgroundColor = bgmColor(244, 244, 244);
        
        self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 140)];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.bgView];
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 100, 30)];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.bgView addSubview:self.titleLabel];
        
        self.txtView = [[UIView alloc]initWithFrame:CGRectMake(15, 50, SCREENW-30, 40)];
        [self.bgView addSubview:self.txtView];
        
    }
    return self;
}
- (void)setTieleAndTxt:(NSString *)title array:(NSArray *)txtArray type:(NSString *)type isPublic:(NSString *)isPublic seleStr:(NSString *)seleStr{
    
    NSArray *seleArray = [seleStr componentsSeparatedByString:@","];

    
    if ([type isEqualToString:@"1"]) {
        
        
        
        self.bgView.frame = CGRectMake(0, 0, SCREENW, 140);
        self.txtView.frame = CGRectMake(15, 50, SCREENW-30, 80);
        for (int i = 0;  i < txtArray.count; i++) {
            int x = i%4;
            int y = i/4;
            float w = 60;
            float SW = (SCREENW-30)/4;
            NSDictionary *dic = [txtArray objectAtIndex:i];
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(SW*x,y*40, w, 30)];
            [btn setTitle:[dic objectForKey:@"name"] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            btn.layer.cornerRadius = 10;
            btn.layer.masksToBounds = YES;
            btn.layer.borderColor = bgmColor(211, 211, 211).CGColor;
            btn.layer.borderWidth = 0.5;
            btn.tag = [[NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]] intValue];
            [btn addTarget:self action:@selector(clickUserBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self.txtView addSubview:btn];
            

            if([isPublic isEqualToString:@"0"]){
                [btn setTitleColor:bgmgrayColor forState:UIControlStateNormal];
                btn.enabled = YES;
                
            }else{
                [btn setTitleColor:bgmsmallColor forState:UIControlStateNormal];
                btn.enabled = NO;
                
            }
            
            for (NSString *sele in seleArray) {
                if([isPublic isEqualToString:@"0"]){
                    if ((i+1) == [sele intValue]) {
                        btn.backgroundColor = bgmyelloeColor;
                         btn.selected = YES;
                    }
                }else{
                    if ((i+1) == [sele intValue]) {
                        btn.backgroundColor = bgmColor(211, 211, 211);
                         btn.selected = YES;
                    }
                }
            }
       
        }
        
        if([isPublic isEqualToString:@"0"]){
            self.titleLabel.textColor = bgmmainColor;
        }else{
            self.titleLabel.textColor = bgmsmallColor;;
        }
        
    }else{
        
        self.bgView.frame = CGRectMake(0, 0, SCREENW, 100);
        self.txtView.frame = CGRectMake(15, 50, SCREENW-30, 40);
        
        
        NSArray *array = @[@"生命接力",@"寻亲团圆",@"爱心助学"];
        for (int i = 0;  i < 3; i++) {
            float w = 70;
            float SW = (SCREENW-30)/4;
            NSString *txt = [array objectAtIndex:i];
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(SW*i,0, w, 30)];
            [btn setTitle:txt forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            btn.layer.cornerRadius = 10;
            btn.layer.masksToBounds = YES;
            btn.layer.borderColor = bgmColor(211, 211, 211).CGColor;
            btn.layer.borderWidth = 0.5;
            btn.tag = i+9;
            [self.txtView addSubview:btn];
            [btn addTarget:self action:@selector(clickPublicBtn:) forControlEvents:UIControlEventTouchUpInside];

            
            if([isPublic isEqualToString:@"1"]){
                [btn setTitleColor:bgmgrayColor forState:UIControlStateNormal];
                btn.enabled = YES;
            }else{
                [btn setTitleColor:bgmsmallColor forState:UIControlStateNormal];
                btn.enabled = NO;

            }
            
            
            
            for (NSString *sele in seleArray) {
                if([isPublic isEqualToString:@"0"]){
                    if ((i+9) == [sele intValue]) {
                        btn.backgroundColor = bgmColor(211, 211, 211);
                        btn.selected = YES;
                    }
                }else{
                    if ((i+9) == [sele intValue]) {
                        btn.backgroundColor = bgmyelloeColor;
                         btn.selected = YES;
                    }
                }
            }
            
            
        }
        
        if([isPublic isEqualToString:@"1"]){
            self.titleLabel.textColor = bgmmainColor;
        }else{
            self.titleLabel.textColor = bgmsmallColor;;
        }
        
    }
    
    self.titleLabel.text = title;
   
   
    
}


- (void)clickUserBtn:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    if ( btn.selected) {
        btn.backgroundColor = bgmyelloeColor;
    }else{
        btn.backgroundColor = [UIColor clearColor];
    }
    
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for(int i = 1;i<= 8;i++){
        UIButton *btn = [self viewWithTag:i];
        if (btn.selected) {
            [array addObject:[NSString stringWithFormat:@"%d",(int)btn.tag]];
        }
    }
    [self.pushDelegate searchUserPushtype:array];

}

- (void)clickPublicBtn:(UIButton *)btn{

    btn.selected = !btn.selected;
    if ( btn.selected) {
        btn.backgroundColor = bgmyelloeColor;
    }else{
        btn.backgroundColor = [UIColor clearColor];
    }
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for(int i = 9;i< 12;i++){
        UIButton *btn = [self viewWithTag:i];
        if (btn.selected) {
            [array addObject:[NSString stringWithFormat:@"%d",(int)btn.tag]];
        }
    }
    [self.pushDelegate searchPublicPushtype:array];

}
@end
